package com.lich.task;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.lich.dao.ClientLoginMapper;
import com.lich.dao.graph.DayLoginTimesMapper;
import com.lich.dao.graph.MonthLoginTimesMapper;
import com.lich.dao.graph.WeekLoginTimesMapper;
import com.lich.dao.graph.base.GraphBaseMapper;
import com.lich.entity.graph.ClientAnalysis;

@Component
@PropertySource("classpath:task.properties")
public class LoginTimesTask {
	private static Logger logger = LoggerFactory.getLogger(LoginTimesTask.class);
	@Resource
	ClientLoginMapper clientLoginMapper;
	@Resource
	DayLoginTimesMapper dayLoginTimesMapper;
	@Resource
	WeekLoginTimesMapper weekLoginTimesMapper;
	@Resource
	MonthLoginTimesMapper monthLoginTimesMapper;
	
	/**
	 * @description:定时任务,添加登陆数 到日,周,月表
	 * @author: yuanyh 
	 * @Time: 2016年9月20日
	 */
	@Scheduled(cron = "${loginTimesTask}")
	public void timedTaskToClientLoginTimes(){
		int result =-1;
		String strDate=beforNumDay(-1);
		System.out.println( "数据日期:"+strDate);
		List<ClientAnalysis> clientAnalysisList = clientLoginMapper.selectDayLoginTimesByDate(strDate);
		if(clientAnalysisList!=null && clientAnalysisList.size()>0){
			
			result = toDayLoginTimes(clientAnalysisList);
			if (result>0){
				logger.info("添加日登陆数成功!数据日期:"+strDate);
			}else{
				logger.warn("添加日登陆数失败!数据日期:"+strDate);
			}
			
			result = toWeekLoginTimes(clientAnalysisList);
			if (result>0){
				logger.info("添加周登陆数成功!数据日期:"+strDate);
			}else{
				logger.warn("添加周登陆数失败!数据日期:"+strDate);
			}
			
			result = toMonthLoginTimes(clientAnalysisList);
			if (result>0){
				logger.info("添加月登陆数成功!数据日期:"+strDate);
			}else{
				logger.warn("添加月登陆数失败!数据日期:"+strDate);
			}
		}
		else{
			logger.warn("客户端信息表没有数据!数据日期:"+strDate);
		}
	}
	/**
	 * @description:添加日登陆数
	 * @author: yuanyh 
	 * @Time: 2016年9月20日  
	 * @param clientAnalysisList 源数据集合
	 * @return
	 */
	public int toDayLoginTimes(List<ClientAnalysis> clientAnalysisList){
			return dayLoginTimesMapper.insertList(clientAnalysisList);
	}
	
	/**
	 * @description:添加周登陆数
	 * @author: yuanyh 
	 * @Time: 2016年9月20日  
	 * @param clientAnalysisList 源数据集合
	 * @return
	 */
	public int toWeekLoginTimes(List<ClientAnalysis> clientAnalysisList){
		int result = -1;
		String strDate = getSunday();
		System.out.println("这周日期:"+strDate);
		List<ClientAnalysis> weekLoginTimesList = weekLoginTimesMapper.selectByDate(strDate);
		for(ClientAnalysis ca:clientAnalysisList){
			ca.setStatisticsDate(getSundayDate());
		}
		if(weekLoginTimesList.size()>0){
			result = saveLoginTimesData(weekLoginTimesMapper,weekLoginTimesList,clientAnalysisList);
		}else{
			result = weekLoginTimesMapper.insertList(clientAnalysisList);
		}
		return result;
	}
	/**
	 * @description:添加月登陆数
	 * @author: yuanyh 
	 * @Time: 2016年9月20日  
	 * @param clientAnalysisList 源数据集合
	 * @return
	 */
	public int toMonthLoginTimes(List<ClientAnalysis> clientAnalysisList){
		int result = -1;
		String strDate = getMonthday();
		System.out.println("这个月日期:"+strDate);
		List<ClientAnalysis> monthLoginTimesList = monthLoginTimesMapper.selectByDate(strDate);
		for(ClientAnalysis ca:clientAnalysisList){
			ca.setStatisticsDate(getMonthdayDate());
		}
		if(monthLoginTimesList.size()>0){
			result = saveLoginTimesData(monthLoginTimesMapper,monthLoginTimesList,clientAnalysisList);
		}else{
			result = monthLoginTimesMapper.insertList(clientAnalysisList);
		}
		return result;
	}
	/**
	 * @description:添加登陆数处理
	 * @author: yuanyh 
	 * @Time: 2016年9月20日  
	 * @param clientAnalysisList 源数据集合
	 * @return
	 */
	public int saveLoginTimesData(GraphBaseMapper<ClientAnalysis> gbm, List<ClientAnalysis> targetList,List<ClientAnalysis> originList){
		int result = -1;
		Boolean isExist = false;
		List<ClientAnalysis> tempOriginList = new ArrayList<ClientAnalysis>();
		for(ClientAnalysis cal : originList){
			for(ClientAnalysis caw : targetList){
				if(cal.getProductId().equals(caw.getProductId())){
					caw.setLoginTimes(cal.getLoginTimes()+caw.getLoginTimes());
					result = gbm.updateLogintimesById(caw);
					if(result<=0){
						return result;
					}
					isExist = true;
				}
			}
			if(!isExist){
				tempOriginList.add(cal);
				isExist = false;
			}
		}
		if(tempOriginList.size()>0){
			result = gbm.insertList(tempOriginList);
		}
		return result;
	}
	
	/**
	* @description: 查询当前日期前x天的日期
    * @author yuanyh
    * @param day 天数（如果day数为负数,说明是此日期前的天数）
    * @return Date 日期
    */
    public static String beforNumDay(int day){
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_YEAR, day);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String newDate = sdf.format(c.getTime());
        return newDate;
    }
    /**
     * @description:获取字符串周日
     * @author: yuanyh 
     * @Time: 2016年9月20日  
     * @return
     */
    public static String getSunday(){
        Calendar c = Calendar.getInstance();
        int day = c.get(Calendar.DAY_OF_WEEK);
        c.add(Calendar.DAY_OF_YEAR, -day+1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String newDate = sdf.format(c.getTime());
        return newDate;
    }
    /**
     * @description:获取周日日期
     * @author: yuanyh 
     * @Time: 2016年9月20日  
     * @return
     */
    public static Date  getSundayDate(){
        Calendar c = Calendar.getInstance();
        int num = c.get(Calendar.DAY_OF_WEEK);
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);
        c.set(year, month, day-num+1, 00, 00, 00);
        return c.getTime();
    }
    /**
     * @description:获取月的第一天日期
     * @author: yuanyh 
     * @Time: 2016年9月20日  
     * @return
     */
    public static Date  getMonthdayDate(){
        Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        c.set(year, month, 1, 00, 00, 00);
        return c.getTime();
    }
    /**
     * @description:获取字符串月的第一天日期
     * @author: yuanyh 
     * @Time: 2016年9月20日  
     * @return
     */
    public static String getMonthday(){
    	String newDate = null;
        Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH)+1;
        if(month<10){
        	String m = "0"+month;
        	newDate = year+"-"+m+"-01";
        }
        else{
        	newDate = year+"-"+month+"-01";
        }
        return newDate;
    } 
    //@Test
    public void text(){
    }
}
